package org.example.spring;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * User: ZhangJuntao
 * Date: 13-10-29
 * Time: 上午11:09
 */
public class Publisher implements ApplicationContextAware {

  private ApplicationContext context;

  @Override
  public void setApplicationContext(ApplicationContext arg0)
      throws BeansException {
    this.context = arg0;
  }

  public void publish(String message) {
    context.publishEvent(new MessageEvent(this, message));
  }

  public static void main(String[] args) {
    ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
    Publisher pub = (Publisher) ctx.getBean("publisher");
    pub.publish("Hello World!");
    pub.publish("The quick brown fox jumped over the lazy dog");
  }
}
